package com.kurumi.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kurumi.mapper.MedicalHomepageMapper;
import com.kurumi.mapper.MedicalRecordMapper;
import com.kurumi.mapper.homepage.MedicalRecordBasicMapper;
import com.kurumi.pojo.MedicalHomepage;
import com.kurumi.service.MedicalHomepageService;
import com.kurumi.service.MedicalRecordDataManageService;
import com.kurumi.util.DateUtil;
import com.kurumi.util.MedicalHomepageUtil;
@Service
public class MedicalHomepageServiceImpl implements MedicalHomepageService {

	@Autowired
	private MedicalHomepageMapper medicalHomepageMapper;
	@Autowired
	private MedicalRecordMapper medicalRecordMapper;
	@Autowired
	private MedicalRecordBasicMapper medicalRecordBasicMapper;
	@Autowired
	private MedicalRecordDataManageService medicalRecordDataManageService;
	
	@Override
	public void syncMedicalHomepage() {
		String endTime = DateUtil.dateFormat("yyyy-MM-dd",  new Date());
		String startTime = DateUtil.getPreMonthDateStr(new Date());
	 // List<String> visitGuids =	medicalHomepageMapper.getSolrUpdates();
		//List<String> visitGuids = medicalRecordBasicMapper.syncMediclRecordBasicVisitGuid(startTime,endTime);
		List<String> visitGuids = medicalRecordDataManageService.getMediclJson(startTime+" 00:00:00",endTime+" 23:59:59");

		MedicalHomepage newMedicalHomepage = null;
	 int updateCount = 0;
	 int addCount = 0;
	  for (String visitGuid : visitGuids) {
		  String json = medicalRecordMapper.getMedicalRecordJsonByGuid(visitGuid);
		  newMedicalHomepage = MedicalHomepageUtil.getMedicalHomepage(json);
		  if(newMedicalHomepage != null){
		  List<MedicalHomepage> homepages = medicalHomepageMapper.selectByVisitGuid(visitGuid);
		  System.out.println();
		  if(CollectionUtils.isEmpty(homepages)){
			  newMedicalHomepage.setRecordTime(new Date());
			  medicalHomepageMapper.insert(newMedicalHomepage);
			  addCount++;
		  }else{
			  for (MedicalHomepage medicalHomepage : homepages) {
				  newMedicalHomepage.setId(medicalHomepage.getId());
				  medicalHomepageMapper.updateByPrimaryKeySelective(newMedicalHomepage);
				  updateCount++;
			 }
		  }
		  
		 
			 
		  }
		  System.out.println("新增数据数量:"+addCount+",更新数据数量:"+updateCount);
		
	}
	}

}
